package com.wyp168.socket.QQ.qqclient.services;

import com.wyp168.socket.QQ.qqclient.common.Message;
import com.wyp168.socket.QQ.qqclient.common.constant.MessageType;
import com.wyp168.socket.QQ.qqclient.utils.Utility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * @ClassName ClientConnectServerThread
 * @Title 客户端保持与服务端连接socket的线程
 * @Author wyp168
 * @Date 2022/10/30 15:07
 */
public class ClientConnectServerThread extends Thread {
    //该线程需要持有Socket
    private Socket socket;
    private String userId;

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public ClientConnectServerThread(String userId, Socket socket) {
        this.userId = userId;
        this.socket = socket;
    }

    @Override
    public void run() {
        //因为Thread需要在后台和服务器通信，因此我们 使用while循环
        while (true) {
            try {
                System.out.println("客户端" + userId + "线程， 等待读取从服务器端发送的消息");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message msg = (Message) ois.readObject();       //如果服务器没有发送Message对象，那么该线程会阻塞在此

                //如果读取的是服务端返回的在线用户列表
                if (msg.getMessageType().equals(MessageType.MESSAGE_RET_ONLINE_USERS)) {
                    //取出在线列表信息，并显示
                    //规定： 如果返回了所有在线用户的id
                    String[] onlineUsers = msg.getContent().split(" ");
                    System.out.println("\n======== 当前在线用户列表如下 ========");
                    for (String onlineUser : onlineUsers) {
                        System.out.println("用户: " + onlineUser);
                    }
                } else if (msg.getMessageType().equals(MessageType.MESSAGE_COMMON_MES)) {   //接收到的是普通的聊天消息
                    //把从服务器端转发的消息显示到控制台即可
                    System.out.println("\n[" + msg.getSentTime() + "]   " +
                            msg.getSender() + "对你说: " + msg.getContent());
                } else if (msg.getMessageType().equals(MessageType.MESSAGE_SEND_TO_ALL)) {  //接收到了群发消息
                    System.out.println("\n[" + msg.getSentTime() + "]   " +
                            msg.getSender() + "说: " + msg.getContent());
                } else if (msg.getMessageType().equals(MessageType.MESSAGE_SEND_FILE)) {  //接收到文件
                    FileOutputStream fileOutputStream = null;
//                    String descPath = "";
                    try {
//                        System.out.print("\n[" + msg.getSentTime() + "]" +
//                                "接收到来自" + msg.getSender() + "的文件，需要保存到哪个位置? (例如:C:/baidu/file.jpg: ");
//                        descPath = Utility.scanner.nextLine();
                        System.out.print("\n[" + msg.getSentTime() + "]" +
                                "接收到来自" + msg.getSender() + "的文件,文件名为: " + msg.getFileName() + ", 正在保存中");
//                        descPath = descPath.charAt(descPath.length() - 1) == '/' ? descPath : descPath + "/";
//
//                        descPath += msg.getFileName();
                        if (msg.getDescPath() != null) {
                            fileOutputStream = new FileOutputStream(new File(msg.getDescPath()));
                            fileOutputStream.write(msg.getBytes());
                        } else {
                            throw new RuntimeException("文件目标路径为空，文件无法保存");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    System.out.println("保存文件到" + msg.getDescPath() + "成功");
                } else if (msg.getMessageType().equals(MessageType.SERVER_PUSH_MESSAGE)) {
                    System.out.println("[" + msg.getSentTime() + "]   " +
                            "服务器" + msg.getSender() + "向您推送内容: " + msg.getContent());
                }


            } catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            }
        }
    }
}
